package fr.gestproj.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.eclipse.persistence.annotations.Index;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import fr.gestproj.commun.annotation.FieldCrudReferentiel;

/** classe entité utilisateur
 *
 * @author Anne */
@Entity
@Table(name = "utilisateur")
public class Utilisateur implements Serializable, UserDetails {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@NotNull
	@Size(max = 150)
	@FieldCrudReferentiel
	@Column(name = "email", length = 150, nullable = false)
	private String email;

	@NotNull
	@Size(max = 128)
	@Column(name = "password", length = 128, nullable = false)
	private String password;

	@Index
	@NotNull
	@Size(max = 50)
	@Column(name = "nom", length = 50, nullable = false)
	private String nom;

	@Index
	@NotNull
	@Size(max = 50)
	@FieldCrudReferentiel
	@Column(name = "prenom", length = 50, nullable = false)
	private String prenom;

	@Size(max = 20)
	@Column(name = "token_modification_password", length = 20, nullable = true)
	private String tokenModificationPassword;

	@NotNull
	@Column(name = "nombre_echec_connexion", nullable = false)
	private int nombreEchecConnexion;

	@NotNull
	@Column(name = "actif", nullable = false)
	private boolean actif;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_creation", nullable = false)
	private Date dateCreation;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_modification_password", nullable = true)
	private Date dateModificationPassword;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_derniere_connexion", nullable = true)
	private Date dateDerniereConnexion;

	@FieldCrudReferentiel
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
	@JoinColumn(name = "ref_profil", nullable = false)
	private Profil profil;

	public Utilisateur() {
		actif = true;
		nombreEchecConnexion = 0;
	}

	public String getNomPrenom() {
		return String.format("%s %s", nom, prenom);
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return profil.getListeRole();
	}

	@Override
	public String getUsername() {
		return email;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return actif;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getTokenModificationPassword() {
		return tokenModificationPassword;
	}

	public void setTokenModificationPassword(String tokenModificationPassword) {
		this.tokenModificationPassword = tokenModificationPassword;
	}

	public int getNombreEchecConnexion() {
		return nombreEchecConnexion;
	}

	public void setNombreEchecConnexion(int nombreEchecConnexion) {
		this.nombreEchecConnexion = nombreEchecConnexion;
	}

	public boolean isActif() {
		return actif;
	}

	public void setActif(boolean actif) {
		this.actif = actif;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Date getDateModificationPassword() {
		return dateModificationPassword;
	}

	public void setDateModificationPassword(Date dateModificationPassword) {
		this.dateModificationPassword = dateModificationPassword;
	}

	public Date getDateDerniereConnexion() {
		return dateDerniereConnexion;
	}

	public void setDateDerniereConnexion(Date dateDerniereConnexion) {
		this.dateDerniereConnexion = dateDerniereConnexion;
	}

	public Profil getProfil() {
		return profil;
	}

	public void setProfil(Profil profil) {
		this.profil = profil;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Utilisateur)) {
			return false;
		}
		Utilisateur other = (Utilisateur) object;
		if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "fr.gestproj.model.Utilisateur[ id=" + id + " ]";
	}

}
